package com.jshoperxms.service.impl;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.jshoperxms.controller.mall.backstage.vo.goods.GoodsPattrsVo;
import com.jshoperxms.controller.mall.backstage.vo.products.ProductPattrsVo;
import com.jshoperxms.controller.mall.backstage.vo.products.ProductSkuVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.jshoperxms.dao.GoodsDetailRpTDao;
import com.jshoperxms.dao.GoodsTDao;
import com.jshoperxms.dao.ProductTDao;
import com.jshoperxms.entity.GoodsDetailRpT;
import com.jshoperxms.entity.GoodsProductskuRpT;
import com.jshoperxms.entity.GoodsT;
import com.jshoperxms.entity.ProductT;
import com.jshoperxms.service.GoodsProductskuRpTService;
import com.jshoperxms.service.GoodsTService;

@Service("goodsTService")
@Scope("prototype")
public class GoodsTServiceImpl extends BaseTServiceImpl<GoodsT>implements GoodsTService {
	@Resource
	private GoodsTDao goodsTDao;
	@Resource
	private GoodsDetailRpTDao goodsDetailRpTDao;
	@Resource
	private ProductTDao productTDao;
	@Resource
	private GoodsProductskuRpTService goodsProductskuRpTService;
	@Resource
	private Serial serial;



	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveGoodsTransactional(GoodsT gt,String detail,String productids) {
		//1，保存商品信息
		this.goodsTDao.save(gt);
		//2，保存商品介绍信息
		GoodsDetailRpT grpt=new GoodsDetailRpT();
		grpt.setId(this.serial.Serialid(SerialKeys.GOODSDETAILRPT));
		grpt.setGoodsid(gt.getGoodsid());
		grpt.setDetail(detail);
		grpt.setCreatetime(DateTools.getSystemTimeBi());
		grpt.setUpdatetime(DateTools.getSystemTimeBi());
		grpt.setCreatorid(BaseTools.getAdminCreateId());
		grpt.setStatus(BaseEnums.DataUsingState.USING.getState());
		grpt.setVersiont(1);
		this.goodsDetailRpTDao.save(grpt);
		//3，更新货物关联的商品信息，即绑定货物到商品主体
		String pids[]=StringUtils.split(productids, TagWords.SPLITDOT);
		for(String s:pids){
			ProductT p=this.productTDao.findByPK(ProductT.class, s);
			if(p!=null){
				p.setGoodsid(gt.getGoodsid());
				this.productTDao.update(p);
			}
			//4,记录商品和货物的sku关系，用于判定是否同一个货物被多个商品占用。
			List<ProductSkuVo>plist= GsonJson.parseJsonToData(p.getPsku(), ProductSkuVo.class);
			for(ProductSkuVo pl:plist){
				GoodsProductskuRpT gpr=new GoodsProductskuRpT();
				gpr.setId(this.serial.Serialid(SerialKeys.GOODSPRODUCTSKURPT));
				gpr.setGoodsid(gt.getGoodsid());
				gpr.setProductid(s);
				gpr.setSkuval(pl.getSkuval());
				gpr.setCreatetime(DateTools.getSystemTimeBi());
				gpr.setUpdatetime(DateTools.getSystemTimeBi());
				gpr.setStatus(BaseEnums.DataUsingState.USING.getState());
				this.goodsProductskuRpTService.save(gpr);
			}
			//5,处理货物的属性,不重复的填充到商品的属性字段中,用于搜索,并添加到搜索引擎中
			Set<GoodsPattrsVo> goodsPattrsVoList=new HashSet<>();
			List<ProductPattrsVo>pattrsVoList=GsonJson.parseJsonToData(p.getPattrs(),ProductPattrsVo.class);
			for (ProductPattrsVo ppv:pattrsVoList){
				GoodsPattrsVo goodsPattrsVo=new GoodsPattrsVo();
				goodsPattrsVo.setGoodsattributeid(ppv.getGoodsattributeid());
				goodsPattrsVo.setGoodsattributename(ppv.getGoodsattributename());
				goodsPattrsVo.setSval(ppv.getSval());
				goodsPattrsVoList.add(goodsPattrsVo);
			}
			//6,更新商品属性
			gt.setPattrs(GsonJson.parseDataToJson(goodsPattrsVoList));
			this.goodsTDao.update(gt);

		}


	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateGoodsTransactional(GoodsT gt,String detail,String productids) {
		//更新商品表
		this.goodsTDao.update(gt);
		//更新商品介绍
		Criterion criterion=Restrictions.eq("goodsid", gt.getGoodsid());
		GoodsDetailRpT gdrt=this.goodsDetailRpTDao.findOneByCriteria(GoodsDetailRpT.class, criterion);
		if(gdrt!=null){
			gdrt.setDetail(detail);
			gdrt.setUpdatetime(DateTools.getSystemTimeBi());
			gdrt.setVersiont(gdrt.getVersiont()+1);
			this.goodsDetailRpTDao.update(gdrt);
		}else{
			gdrt=new GoodsDetailRpT();
			gdrt.setCreatetime(DateTools.getSystemTimeBi());
			gdrt.setUpdatetime(DateTools.getSystemTimeBi());
			gdrt.setCreatorid(BaseTools.getAdminCreateId());
			gdrt.setStatus(BaseEnums.DataUsingState.USING.getState());
			gdrt.setVersiont(1);
			gdrt.setGoodsid(gt.getGoodsid());
			gdrt.setId(this.serial.Serialid(SerialKeys.GOODSDETAILRPT));
			this.goodsDetailRpTDao.save(gdrt);
		}

		//删除所有商品和货物的规格值关系再添加
		Criterion criterion2=Restrictions.eq("goodsid", gt.getGoodsid());
		List<GoodsProductskuRpT>glist=this.goodsProductskuRpTService.findByCriteria(GoodsProductskuRpT.class, criterion2);
		for(GoodsProductskuRpT gp:glist){
			gp.setStatus(BaseEnums.DataUsingState.DEL.getState());
			gp.setUpdatetime(DateTools.getSystemTimeBi());
			this.goodsProductskuRpTService.update(gp);
		}

		String pids[]=StringUtils.split(productids, TagWords.SPLITDOT);
		for(String s:pids){
			ProductT p=this.productTDao.findByPK(ProductT.class, s);
			if(p!=null){
				p.setGoodsid(gt.getGoodsid());
				this.productTDao.update(p);
			}
			//4,记录商品和货物的sku关系，用于判定是否同一个货物被多个商品占用。
			List<ProductSkuVo>plist=GsonJson.parseJsonToData(p.getPsku(), ProductSkuVo.class);
			for(ProductSkuVo pl:plist){
				GoodsProductskuRpT gpr=new GoodsProductskuRpT();
				gpr.setId(this.serial.Serialid(SerialKeys.GOODSPRODUCTSKURPT));
				gpr.setGoodsid(gt.getGoodsid());
				gpr.setProductid(s);
				gpr.setSkuval(pl.getSkuval());
				gpr.setCreatetime(DateTools.getSystemTimeBi());
				gpr.setUpdatetime(DateTools.getSystemTimeBi());
				gpr.setStatus(BaseEnums.DataUsingState.USING.getState());
				this.goodsProductskuRpTService.save(gpr);
			}
			//5,处理货物的属性,不重复的填充到商品的属性字段中,用于搜索,并添加到搜索引擎中
			Set<GoodsPattrsVo> goodsPattrsVoList=new HashSet<>();
			List<ProductPattrsVo>pattrsVoList=GsonJson.parseJsonToData(p.getPattrs(),ProductPattrsVo.class);
			for (ProductPattrsVo ppv:pattrsVoList){
				GoodsPattrsVo goodsPattrsVo=new GoodsPattrsVo();
				goodsPattrsVo.setGoodsattributeid(ppv.getGoodsattributeid());
				goodsPattrsVo.setGoodsattributename(ppv.getGoodsattributename());
				goodsPattrsVo.setSval(ppv.getSval());
				goodsPattrsVoList.add(goodsPattrsVo);
			}
			//6,更新商品属性
			gt.setPattrs(GsonJson.parseDataToJson(goodsPattrsVoList));
			this.goodsTDao.update(gt);
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void delGoodsTransactional(String goodsids) {
		//更新商品
		String ids[]=StringUtils.split(goodsids, TagWords.SPLITDOT);
		for(String s:ids){
			GoodsT gt=this.goodsTDao.findByPK(GoodsT.class, s);
			if(gt!=null){
				gt.setStatus(BaseEnums.DataUsingState.DEL.getState());
				gt.setUpdatetime(DateTools.getSystemTimeBi());
				this.goodsTDao.update(gt);
				Criterion criterion=Restrictions.eq("goodsid", gt.getGoodsid());
				List<ProductT>plist=this.productTDao.findByCriteria(ProductT.class, criterion);
				for(ProductT p:plist){
					p.setGoodsid(StaticKey.EMPTY);
					this.productTDao.update(p);
				}
				GoodsDetailRpT gdrt=this.goodsDetailRpTDao.findOneByCriteria(GoodsDetailRpT.class, criterion);
				if(gdrt!=null){
					gdrt.setStatus(BaseEnums.DataUsingState.DEL.getState());
					gdrt.setUpdatetime(DateTools.getSystemTimeBi());
					this.goodsDetailRpTDao.update(gdrt);
				}
				//删除所有商品和货物的规格值关系再添加
				Criterion criterion2=Restrictions.eq("goodsid", gt.getGoodsid());
				List<GoodsProductskuRpT>glist=this.goodsProductskuRpTService.findByCriteria(GoodsProductskuRpT.class, criterion2);
				for(GoodsProductskuRpT gp:glist){
					gp.setStatus(BaseEnums.DataUsingState.DEL.getState());
					gp.setUpdatetime(DateTools.getSystemTimeBi());
					this.goodsProductskuRpTService.update(gp);
				}
			}
		}

	}




}
